import Object.*;
import Util.HttpUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

/**
 * @author jklu
 * 程序运行入口
 * main
 */
public class Launcher {
    public static void main(String[] args) throws IOException{

        Scanner scanner = new Scanner(System.in);
        //搜索关键字
        String word = null;
        //请求页数、起始地址--->用于标记每一页
        int page = 1, start = 0;
        System.out.println("Please input the key word : ");
        word = scanner.nextLine();
        //URL编码
        word =  java.net.URLEncoder.encode(word,"UTF-8");
        //设置代理
        System.out.println("Please input the proxy ip :");
        HttpUtil.ip = scanner.next();
        System.out.println("Please input the proxy port :");
        HttpUtil.port = scanner.nextInt();
        //选择搜索引擎cm
        System.out.println("please choose the search engine (1.baidu 2.FlipBaidu 3.Google 4.Bing) :");
        int choice = scanner.nextInt();
        //百度
        if (choice == 1) {
            BaiduSpider spider = new BaiduSpider();
            int color = 0;
            System.out.println("0全部颜色 1红色 2黄色 4绿色 8青色 16蓝色 32紫色 64粉色 128棕色 256橙色 512黑色 1024白色 2048黑白 ");
            System.out.println("Please input the color : ");
            color = scanner.nextInt();
            System.out.println("Please input the execute count (per execute will download 60 pictures ) : ");
            page = scanner.nextInt();
            //请求n页信息
            for (int i = 0; i < page; i++) {
                //获取每一页的所有图片信息，一页60张图片
                BaiduPic pic = spider.getBaiduPic(word, start, 60, color);
                System.out.println("--->" + pic.getPicList().size());
                //下载该页中所有图片
                spider.downloadBaiduPic(pic);
                //变为下一页的起始地址
                start += 60;
            }
            //Flip接口的Baidu
        } else if (choice == 2) {
            BaiduSpider2 spider = new BaiduSpider2();
            int color = 0;
            //总共获取的页数
            System.out.println("Please input the execute count (per execute will download 60 (max) pictures ) : ");
            page = scanner.nextInt();
            //请求n页信息
            for (int i = 0; i < page; i++) {
                //获取每一页的所有图片信息，一页60张图片，但实际下载的不足60
                BaiduPic2 pic = spider.getBaiduPic(word,start);
                System.out.println("--->" + pic.getPicList().size());
                //下载该页中所有图片
                spider.downloadAll(pic);
                //变为下一页的起始地址
                start += 60;
            }
            //Google
        }else if (choice == 3) {
            GoogleSpider spider = new GoogleSpider();
            int color = 0;
            //使用代理--->下载图片
            boolean useProxy = true;
            //颜色
            System.out.println("0全部颜色  2黑白 4透明 8红色 16橘色 32绿色 64青色 128蓝色 256紫色 512粉色 1024白色 2048灰色 4096黑色 8192棕色 ");
            System.out.println("Please input the color : ");
            color = scanner.nextInt();
            //总共获取的页数
            System.out.println("The max execute count is 8");
            System.out.println("Please input the execute count (per execute will download 100 (max) pictures ) : ");
            page = scanner.nextInt();
            while (page > 8) {
                System.out.println("The max execute count is 8");
                page = scanner.nextInt();
            }
            //是否使用代理下载图片
            System.out.println("use proxy to download the pictures ( true or false )? ");
            useProxy = scanner.nextBoolean();
            //请求n页信息
            for (int i = 0; i < page; i++) {
                //获取每一页的所有图片信息，一页100张图片，但实际下载的不足100
                GooglePic pic = spider.getPicList(word, i, start, color);
                System.out.println("--->" + pic.getPicList().size());
                //下载该页中所有图片
                spider.downloadAllPic(pic, useProxy);
                //变为下一页的起始地址
                start += 100;
            }
            //Bing
        }else if(choice==4){
            //和谷歌类似
            BingSpider bingSpider = new BingSpider();
            int color = 0;boolean useProxy = false;
            System.out.println("0全部颜色 1红色 2黄色 4绿色 8青色 16蓝色 32紫色 64粉色 128棕色 256橙色 512黑色 1024白色 2048黑白 ");
            System.out.println("Please input the color : ");
            color = scanner.nextInt();
            System.out.println("Please input the execute count (per execute will download 100(max) pictures ) : ");
            page = scanner.nextInt();
            System.out.println("use proxy to download the pictures ( true or false )? ");
            useProxy = scanner.nextBoolean();
            for (int i = 0;i<page;i++){
                BingPic pic = bingSpider.getBingPic(word.replace(" ", "%20"),start,100,color);
                System.out.println("--->" + pic.getPicList().size());
                bingSpider.downloadBingPic(pic,useProxy);
                start+=100;
            }
        }
    }

}
